package br.perfiman.service.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.ce.wcaquino.Validation.GeneralValidation;
import br.perfiman.dao.AbstractDAOService;
import br.perfiman.dao.CreditCardDAOService;
import br.perfiman.exceptions.ErrorsConstants;
import br.perfiman.exceptions.InvalidFieldException;
import br.perfiman.exceptions.RequiredFieldException;
import br.perfiman.model.CreditCard;
import br.perfiman.service.CreditCardService;

/**
 *
 * @author Wagner Costa
 * @since 27/12/2007
 * @version 1.0
 *
 */
@Stateless
public class CreditCardServiceBean extends AbstractServiceBean<CreditCard> implements CreditCardService{
	@EJB(name="CreditCardDAOService")
	protected CreditCardDAOService dao;

	@Override
	public AbstractDAOService<CreditCard> getDao() {
		return dao;
	}

	@Override
	public CreditCard insert(CreditCard entity) throws Exception {
		return super.insert(entity);
	}

	@Override
	public void remove(CreditCard entity) throws Exception {
		super.remove(entity);
	}

	@Override
	public List<CreditCard> retrieveAll() throws Exception {
		return super.retrieveAll();
	}

	@Override
	public CreditCard retrieveById(Object id) throws Exception {
		return super.retrieveById(id);
	}

	@Override
	public CreditCard update(CreditCard entity) throws Exception {
		return super.update(entity);
	}

	@Override
	public void validateBussinessRules(CreditCard entity, boolean isUpdate) {
		// TODO Auto-generated method stub

	}

	@Override
	public void validateInvalidFields(CreditCard entity, boolean isUpdate) {
		InvalidFieldException exception = new InvalidFieldException();
		if(entity.getCardLimit() < 0)
			exception.add(getFormKey("cardLimit"), ErrorsConstants.GREATER_THAN, "0");
		if(entity.getGoodDay() >= 1 && entity.getGoodDay()<= 31)
			exception.add(getFormKey("goodDay"), ErrorsConstants.BEETWEN, "0", "31");
		if(entity.getGoodDay() >= 1 && entity.getGoodDay()<= 31)
			exception.add(getFormKey("payment"), ErrorsConstants.BEETWEN, "0", "31");

		if(exception.hasErrors())
			throw exception;
	}

	@Override
	public void validateRequiredFields(CreditCard entity, boolean isUpdate) {
		RequiredFieldException exception = new RequiredFieldException();
		if(GeneralValidation.isEmpty(entity.getName()))
			exception.add(getFormKey("name"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getCardLimit()))
			exception.add(getFormKey("cardLimit"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getGoodDay()))
			exception.add(getFormKey("goodDay"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getPayment()))
			exception.add(getFormKey("payment"), ErrorsConstants.REQUIRED);

		if(exception.hasErrors())
			throw exception;
	}
}
